dobro=function ()
{
num<<-num*2
print(num)
}




#calculates all possible combinations of 3 elements when each element comes from a different set 

comb_3=function(pop1,pop2,pop3)
	{
	result=matrix(NA,3)
	for (i in pop1)
		{
		for (j in pop2)
			{
			for(k in pop3)
				{
				ijk=c(i,j,k)
				result=cbind(result,ijk)
				}
			}
		}
	result=result[,-1]	
	return(result)
	}
	
	
#calculates all possible combinations of 4 elements when each element comes from a different set 
comb_4=function(pop1,pop2,pop3,pop4)
	{
	result=matrix(NA,4)
	for (i in pop1)
		{
		for (j in pop2)
			{
			for(k in pop3)
				{
				for(l in pop4)
					{
					ijk=c(i,j,k)
					result=cbind(result,ijk)
					}
				}
			}
		}
	result=result[,-1]	
	return(result)
	}
	
#calculates all the subtrees from an input set and returns a table with the occurencies of each pattern in the input set
subtrees=function(sub_set)
	{
	for (s in 1:dim(sub_set)[2])
		{
		#count_tot=count_tot+1
		subtr_nw=NULL # restart subtr_nw
		subtr=subset(my3_phylo4,tips.include=sub_set[,s])
		all_subtr[[paste(x,s,sep="_")]]<<-subtr
		subtr_phylo=as(subtr,"phylo")
		subtr_phylo$tip.label=substr(subtr_phylo$tip.label,9,9)
		subtr_phylo$edge.length=NULL
		subtr_newick=write.tree(subtr_phylo)
		subtr_nw=table_patterns[table_patterns[,1]==subtr_newick,2]
		patterns[pat1==x,pat2==subtr_nw]=patterns[pat1==x,pat2==subtr_nw]+1
		}
	#patterns[pat1==x,pat2=="total"]=count_tot
	return(patterns)
	}	
